package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.GasAlertorModeAttrHander;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <Description> <br>
 *
 * @author zhouJiao<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年11月22日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(GasAlertorModeAttrHander.class)
public class GasAlertorModeAttrLinkHander extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String attrValue = attr.getValue();
        String custId = data.getCustId();
        String prodInstId = inst.getInstId();
        //销售价处理
        KiteAttr sellAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004107");//200004107为销售价属性编码
        //结算价处理
        KiteAttr settleAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004108");
        //公允价处理
        KiteAttr evenhAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004115");
        //物资编码
        KiteAttr materialsAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004178");

        String sceneInstId = data.getSceneInstId();
        // 框架主产品销售价处理
        if (Objects.nonNull(sellAttr)) {
            //型号的枚举值110010295(雁飞民用燃气报警器—标配款甲烷)
            setHandleAttrValue ("110010295", attrValue, inst, "200004107", "230", sceneInstId);//key:型号枚举值ID  newFieldName:结算价的属性编码   newValue:结算价的枚举值ID
            //110010296(雁飞民用燃气报警器—标配款丙烷)
            setHandleAttrValue ("110010296", attrValue, inst, "200004107", "230", sceneInstId);
            //110010297(雁飞民用燃气报警器—高配款)
            setHandleAttrValue ("110010297", attrValue, inst, "200004107", "300", sceneInstId);
        }

        // 框架主产品结算价处理
        if (Objects.nonNull(settleAttr)) {
            //110010263(雁飞民用燃气报警器—标配款甲烷（199元）)
            setHandleAttrValue ("110010295", attrValue, inst, "200004108", "199", sceneInstId);//key:型号枚举值ID  newFieldName:结算价的属性编码   newValue:结算价的枚举值ID
            //110010296(雁飞民用燃气报警器—标配款丙烷)
            setHandleAttrValue ("110010296", attrValue, inst, "200004108", "199", sceneInstId);
            //110010297(雁飞民用燃气报警器—高配款)
            setHandleAttrValue ("110010297", attrValue, inst, "200004108", "259", sceneInstId);

        }

        // 燃气报警器终端产品公允价处理
        if (Objects.nonNull(evenhAttr)) {
            setHandleAttrValue ("110010295", attrValue, inst, "200004115", "179", sceneInstId);//key:型号枚举值ID  newFieldName:公允价属性编码   newValue:公允价枚举值编码

            setHandleAttrValue ("110010296", attrValue, inst, "200004115", "179", sceneInstId);

            setHandleAttrValue ("110010297", attrValue, inst, "200004115", "239", sceneInstId);

        }

        // 燃气报警器终端产品物资编码处理
        if (Objects.nonNull(materialsAttr)) {
            setHandleAttrValue ("110010295", attrValue, inst, "200004178", "7600301489", sceneInstId);//key:型号枚举值ID  newFieldName:物资编码属性编码   newValue:物资编码枚举值编码

            setHandleAttrValue ("110010296", attrValue, inst, "200004178", "7600301489", sceneInstId);

            setHandleAttrValue ("110010297", attrValue, inst, "200004178", "7600301488", sceneInstId);

        }

        // 主框架型号联动燃气报警器终端型号
        setOthProdInstMode(attrValue, inst, sceneInstId);


    }
    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    private void setHandleAttrValue (String key, String oldValue, KiteInst inst, String newFieldName,
                                     String newValue, String sceneInstId ) {
        if (KiteStringUtils.isEqual(key, oldValue)) {
            operateInstAttrService.setAttrValue(inst, newFieldName, newValue, sceneInstId, null);
        }
    }


    private void setOthProdInstMode(String value, KiteInst inst, String sceneInstId) {
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
        kiteProdInstList.stream().forEach(prodInst ->{
            // 找到型号
            KiteAttr kiteAttr = prodInst.getAttr("200002772");
            if (Objects.nonNull(kiteAttr) && !KiteStringUtils.isEqual(inst.getInstId(), prodInst.getProdInstId()) ) {
                String kiteAttrValue = kiteAttr.getAttrId();
                //雁飞智慧燃气管理平台的型号无需联动
                if ( ! "202111241752".equals(kiteAttrValue) && KiteStringUtils.equalsAny(value, "110010295", "110010296","110010297")) {//value是主框架型号枚举值id
                    operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), inst.getAttrValue("200002772"), sceneInstId, null);
                }
            }

        });
    }

}
